<template>
  <div>
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="freeInline" class="demo-form-inline">
      <el-form-item label="标题名称">
        <el-input
          clearable
          @clear="queryFree"
          v-model="freeInline.name"
          placeholder="请输入标题名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryFree">搜索</el-button>
        <el-button type="primary" @click="toInsert">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格内容 -->
    <el-table :data="freeList" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="标题"> </el-table-column>
      <el-table-column prop="img" label="图片">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="
              'http://localhost:8888/sfzy/downLoad?filename=' + scope.row.img
            "
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="simplyIntroduce" label="简介"></el-table-column>
      <el-table-column prop="text" label="详情">
        <template slot-scope="scope">
          <el-link type="primary" @click="toCheck(scope.row)">点击查看</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link type="primary" @click="toUpdate(scope.row)">编辑</el-link>
          <el-link type="primary" @click="toDelete(scope.row.id)">删除</el-link>
        </template>
        <!-- <el-link type="primary" @click="toUpdate">编辑</el-link>
        <el-link type="primary">删除</el-link> -->
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="freeInline.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 查看详情 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible1"
      @close="cancal"
    >
      <el-form :model="free">
        <el-form-item label="详情" label-width="80px">
          <!-- <el-input v-model="free.text" placeholder="请输入详情"></el-input> -->
          <!-- 富文本编辑器组件 -->
          <!-- quill-editor标签 -->
          <quill-editor v-model="free.text" :disabled="true"></quill-editor>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 新增修改弹出框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancal">
      <el-form :model="free">
        <el-form-item label="标题" label-width="80px">
          <el-input v-model="free.name" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="80px">
          <!-- <el-input v-model="free.img" placeholder="请输入图片"></el-input> -->
          <el-upload
            action="http://localhost:8888/sfzy/upload"
            list-type="picture-card"
            :file-list="fileList"
            :on-success="handleSuccess"
            :limit="1"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="简介" label-width="80px">
          <el-input
            v-model="free.simplyIntroduce"
            placeholder="请输入简介"
          ></el-input>
        </el-form-item>
        <el-form-item label="详情" label-width="80px">
          <!-- <el-input v-model="free.text" placeholder="请输入详情"></el-input> -->
          <!-- 富文本编辑器组件 -->
          <!-- quill-editor标签 -->
          <quill-editor v-model="free.text"></quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancal">取 消</el-button>
        <el-button type="primary" @click="insertAndUpdateSuccess"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      freeInline: {
        name: "",
        pageNum: 1,
        pageSize: 5,
      },
      freeList: [],
      total: 6,
      title: "",
      free: {
        id: "",
        name: "",
        img: "",
        simplyIntroduce: "",
        text: "",
        createTime: "",
      },
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogImageUrl: "", // 预览图片路径
      dialogVisible: false, // 预览图片的对话框
      fileList: [], //图片预览
    };
  },
  methods: {
    toCheck(row) {
      this.title = "详情内容";
      this.free = row;
      this.dialogFormVisible1 = true;
    },
    // 处理上传成功的函数
    handleSuccess(response) {
      // 把上传后的图片路径，交给文章对象的img
      this.free.img = response.message;
    },

    // 删除图片的事件
    handleRemove(file, fileList) {
      console.log(file, fileList);
      // 移除数据
      this.free.img = "";
    },

    // 预览图片事件
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //取消清空
    cancal() {
      this.dialogFormVisible = false;
      this.free = {
        id: "",
        name: "",
        img: "",
        simplyIntroduce: "",
        text: "",
        createTime: "",
      };
      this.queryFree();
    },
    //删除
    toDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("free/" + id).then((res) => {
            if (res.data.status == 200) {
              this.$message.success("删除成功");
              this.queryFree();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //前往编辑页面
    toUpdate(row) {
      this.title = "编辑";
      //赋值
      this.free = row;
      this.dialogFormVisible = true;
      this.fileList = [
        {
          name: row.img,
          url: "http://localhost:8888/sfzy/downLoad?filename=" + row.img,
        },
      ];
    },
    //添加和修改
    insertAndUpdateSuccess() {
      if (this.free.id) {
        this.$http.put("free", this.free).then((res) => {
          if (res.data.status == 200) {
            this.$message.success("修改成功");
            this.free = {
              id: "",
              name: "",
              img: "",
              simplyIntroduce: "",
              text: "",
              createTime: "",
            };
            //关闭对话框
            this.dialogFormVisible = false;
            //从新查询
            this.queryFree();
          } else {
            this.$message.error("修改失败");
          }
        });
      } else {
        this.$http.post("free", this.free).then((res) => {
          if (res.data.status == 200) {
            this.$message.success("添加成功");
            this.free = {
              id: "",
              name: "",
              img: "",
              simplyIntroduce: "",
              text: "",
              createTime: "",
            };
            //关闭对话框
            this.dialogFormVisible = false;
            //从新查询
            this.queryFree();
          } else {
            this.$message.error("添加失败");
          }
        });
      }
      this.fileList = [];
    },
    //前往添加页面
    toInsert() {
      this.title = "新增";
      this.dialogFormVisible = true;
    },
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.freeInline.pageSize = val;
      this.freeInline.pageNum = 1;
      this.queryFree();
    },
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.freeInline.pageNum = val;
      this.queryFree();
    },
    queryFree() {
      this.$http
        .get("free", {
          params: this.freeInline,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.freeList = res.data.data.list;
            this.total = res.data.data.total;
          } else {
            this.$message.error("查询失败");
          }
        });
    },
  },
  mounted() {
    this.queryFree();
  },
};
</script>
<style>
.el-pagination {
  margin-top: 15px;
}
.el-link {
  padding: 0 8px;
}
.ql-container {
  min-height: 200px;
}
</style>